package cn.changsheng.csfenjie.utils;

import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.Server.Spigot;
import org.bukkit.entity.Player;

public class BungeeUtil
{
    public static void sendShowMessage(final Player player, final String message, final String showmessage) {
        final TextComponent send = new TextComponent(ChatColor.translateAlternateColorCodes('&', message));
        send.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(ChatColor.translateAlternateColorCodes('&', showmessage)).create()));
        if (player == null) {
            Bukkit.spigot().broadcast((BaseComponent)send);
            return;
        }
        player.spigot().sendMessage((BaseComponent)send);
    }
    
    public static void sendShowItemMessage(final Player player, final String message, final String showmessage) {
        final TextComponent send = new TextComponent(ChatColor.translateAlternateColorCodes('&', message));
        send.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_ITEM, new ComponentBuilder(ChatColor.translateAlternateColorCodes('&', showmessage)).create()));
        if (player == null) {
            Bukkit.spigot().broadcast((BaseComponent)send);
            return;
        }
        player.spigot().sendMessage((BaseComponent)send);
    }
    
    public static void sendPartShowMessage(final Player player, String message, String part, final String changemessage, final String showmessage) {
        part = part.replace("[", "").replace("]", "");
        message = message.replace("[", "").replace("]", "");
        if (message.contains(part)) {
            final TextComponent all = new TextComponent("");
            boolean first = true;
            while (message.contains(part)) {
                if (first) {
                    try {
                        all.addExtra((BaseComponent)new TextComponent(ChatColor.translateAlternateColorCodes('&', message.split(part)[0])));
                    }
                    catch (ArrayIndexOutOfBoundsException ex) {}
                }
                final TextComponent parts = new TextComponent(new TextComponent(ChatColor.translateAlternateColorCodes('&', part.replace(part, changemessage))));
                parts.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(ChatColor.translateAlternateColorCodes('&', showmessage)).create()));
                all.addExtra((BaseComponent)parts);
                try {
                    all.addExtra((BaseComponent)new TextComponent(ChatColor.translateAlternateColorCodes('&', message.split(part)[1])));
                }
                catch (ArrayIndexOutOfBoundsException ex2) {}
                first = false;
                message = message.replaceFirst(part, "");
            }
            if (player == null) {
                Bukkit.spigot().broadcast((BaseComponent)all);
                return;
            }
            player.spigot().sendMessage((BaseComponent)all);
        }
        else {
            player.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
        }
    }
    
    public static void sendPartShowItemMessage(final Player player, String message, String part, final String changemessage, final String showitem) {
        part = part.replace("[", "").replace("]", "");
        message = message.replace("[", "").replace("]", "");
        if (message.contains(part)) {
            final TextComponent all = new TextComponent("");
            boolean first = true;
            while (message.contains(part)) {
                if (first) {
                    try {
                        all.addExtra((BaseComponent)new TextComponent(ChatColor.translateAlternateColorCodes('&', message.split(part)[0])));
                    }
                    catch (ArrayIndexOutOfBoundsException ex) {}
                }
                final TextComponent parts = new TextComponent(new TextComponent(ChatColor.translateAlternateColorCodes('&', part.replace(part, changemessage))));
                parts.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_ITEM, new ComponentBuilder(ChatColor.translateAlternateColorCodes('&', showitem)).create()));
                all.addExtra((BaseComponent)parts);
                try {
                    all.addExtra((BaseComponent)new TextComponent(ChatColor.translateAlternateColorCodes('&', message.split(part)[1])));
                }
                catch (ArrayIndexOutOfBoundsException ex2) {}
                first = false;
                message = message.replaceFirst(part, "");
            }
            if (player == null) {
                Bukkit.spigot().broadcast((BaseComponent)all);
                return;
            }
            player.spigot().sendMessage((BaseComponent)all);
        }
        else {
            player.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
        }
    }
    
    public static void sendCommandMessage(final Player player, final String message, final String showmessage, final String command, final Boolean usecommand) {
        final TextComponent send = new TextComponent(ChatColor.translateAlternateColorCodes('&', message));
        String value = null;
        if (usecommand) {
            value = "RUN_COMMAND";
        }
        else {
            value = "SUGGEST_COMMAND";
        }
        send.setClickEvent(new ClickEvent(ClickEvent.Action.valueOf(value), command));
        send.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(ChatColor.translateAlternateColorCodes('&', showmessage)).create()));
        if (player == null) {
            Bukkit.spigot().broadcast((BaseComponent)send);
            return;
        }
        player.spigot().sendMessage((BaseComponent)send);
    }
    
    public static void sendCommandItemMessage(final Player player, final String message, final String showitem, final String command, final Boolean usecommand) {
        final TextComponent send = new TextComponent(ChatColor.translateAlternateColorCodes('&', message));
        String value = null;
        if (usecommand) {
            value = "RUN_COMMAND";
        }
        else {
            value = "SUGGEST_COMMAND";
        }
        send.setClickEvent(new ClickEvent(ClickEvent.Action.valueOf(value), command));
        send.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_ITEM, new ComponentBuilder(ChatColor.translateAlternateColorCodes('&', showitem)).create()));
        if (player == null) {
            Bukkit.spigot().broadcast((BaseComponent)send);
            return;
        }
        player.spigot().sendMessage((BaseComponent)send);
    }
    
    public static void sendPartShowCommandMessage(final Player player, String message, String part, final String changemessage, final String showmessage, final String command, final Boolean usecommand) {
        part = part.replace("[", "").replace("]", "");
        message = message.replace("[", "").replace("]", "");
        if (message.contains(part)) {
            final TextComponent all = new TextComponent("");
            boolean first = true;
            while (message.contains(part)) {
                if (first) {
                    try {
                        all.addExtra((BaseComponent)new TextComponent(ChatColor.translateAlternateColorCodes('&', message.split(part)[0])));
                    }
                    catch (ArrayIndexOutOfBoundsException ex) {}
                }
                final TextComponent parts = new TextComponent(new TextComponent(ChatColor.translateAlternateColorCodes('&', part.replace(part, changemessage))));
                parts.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(ChatColor.translateAlternateColorCodes('&', showmessage)).create()));
                String value = null;
                if (usecommand) {
                    value = "RUN_COMMAND";
                }
                else {
                    value = "SUGGEST_COMMAND";
                }
                parts.setClickEvent(new ClickEvent(ClickEvent.Action.valueOf(value), command));
                all.addExtra((BaseComponent)parts);
                try {
                    all.addExtra((BaseComponent)new TextComponent(ChatColor.translateAlternateColorCodes('&', message.split(part)[1])));
                }
                catch (ArrayIndexOutOfBoundsException ex2) {}
                first = false;
                message = message.replaceFirst(part, "");
            }
            if (player == null) {
                Bukkit.spigot().broadcast((BaseComponent)all);
                return;
            }
            player.spigot().sendMessage((BaseComponent)all);
        }
        else {
            player.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
        }
    }
    
    public static void sendPartShowCommandItemMessage(final Player player, String message, String part, final String changemessage, final String showitem, final String command, final Boolean usecommand) {
        part = part.replace("[", "").replace("]", "");
        message = message.replace("[", "").replace("]", "");
        if (message.contains(part)) {
            final TextComponent all = new TextComponent("");
            boolean first = true;
            while (message.contains(part)) {
                if (first) {
                    try {
                        all.addExtra((BaseComponent)new TextComponent(ChatColor.translateAlternateColorCodes('&', message.split(part)[0])));
                    }
                    catch (ArrayIndexOutOfBoundsException ex) {}
                }
                final TextComponent parts = new TextComponent(new TextComponent(ChatColor.translateAlternateColorCodes('&', part.replace(part, changemessage))));
                parts.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_ITEM, new ComponentBuilder(ChatColor.translateAlternateColorCodes('&', showitem)).create()));
                String value = null;
                if (usecommand) {
                    value = "RUN_COMMAND";
                }
                else {
                    value = "SUGGEST_COMMAND";
                }
                parts.setClickEvent(new ClickEvent(ClickEvent.Action.valueOf(value), command));
                all.addExtra((BaseComponent)parts);
                try {
                    all.addExtra((BaseComponent)new TextComponent(ChatColor.translateAlternateColorCodes('&', message.split(part)[1])));
                }
                catch (ArrayIndexOutOfBoundsException ex2) {}
                first = false;
                message = message.replaceFirst(part, "");
            }
            if (player == null) {
                Bukkit.spigot().broadcast((BaseComponent)all);
                return;
            }
            player.spigot().sendMessage((BaseComponent)all);
        }
        else {
            player.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
        }
    }
    
    public static BaseComponent TransformPartShowCommandMessage(String message, String part, final String changemessage, final String showmessage, final String command, final Boolean usecommand) {
        part = part.replace("[", "").replace("]", "");
        message = message.replace("[", "").replace("]", "");
        if (message.contains(part)) {
            final TextComponent all = new TextComponent("");
            boolean first = true;
            while (message.contains(part)) {
                if (first) {
                    try {
                        all.addExtra((BaseComponent)new TextComponent(ChatColor.translateAlternateColorCodes('&', message.split(part)[0])));
                    }
                    catch (ArrayIndexOutOfBoundsException ex) {}
                }
                final TextComponent parts = new TextComponent(new TextComponent(ChatColor.translateAlternateColorCodes('&', part.replace(part, changemessage))));
                parts.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(ChatColor.translateAlternateColorCodes('&', showmessage)).create()));
                String value = null;
                if (usecommand) {
                    value = "RUN_COMMAND";
                }
                else {
                    value = "SUGGEST_COMMAND";
                }
                parts.setClickEvent(new ClickEvent(ClickEvent.Action.valueOf(value), command));
                all.addExtra((BaseComponent)parts);
                try {
                    all.addExtra((BaseComponent)new TextComponent(ChatColor.translateAlternateColorCodes('&', message.split(part)[1])));
                }
                catch (ArrayIndexOutOfBoundsException ex2) {}
                first = false;
                message = message.replaceFirst(part, "");
            }
            return (BaseComponent)all;
        }
        return (BaseComponent)new TextComponent(message);
    }
}
